export class GameAction_nbddz extends cc.Component {

    private static countdownHandler = [];

    private static lightPosition(light:cc.Node,range:number){
        let h = light.parent.height;
        let w = light.parent.width;
        if(range>0.875){
            light.x = Math.tan(360 * (1-range) * Math.PI/180) * h/2;
            light.y = h/2;
        }else if(range>0.75){
            light.x = w/2;
            light.y = h/2 - Math.tan(360 * (0.875-range) * Math.PI/180) * w/2;
        }else if(range>0.625){
            light.x = w/2;
            light.y = -Math.tan(360 * (0.75-range) * Math.PI/180) * w/2;
        }else if(range>0.5){
            light.x = w/2 - Math.tan(360 * (0.625-range) * Math.PI/180) * h/2;
            light.y = -h/2;
        }else if(range>0.375){
            light.x = -Math.tan(360 * (0.5-range) * Math.PI/180) * h/2;
            light.y = -h/2;
        }else if(range>0.25){
            light.x = -w/2;
            light.y = -h/2 - (-Math.tan(360 * (0.375-range) * Math.PI/180) * w/2);
        }else if(range>0.125){
            light.x = -w/2;
            light.y = Math.tan(360 * (0.25-range) * Math.PI/180) * w/2;
        }else{
            light.x = -Math.tan(360 * (range) * Math.PI/180) * h/2;
            light.y = h/2;
        }
    }
    /**
     * 倒计时
     * @param sp
     */
    static countdown(seat:number,node:cc.Node,time:number,remain:number){
        console.log("countdown",time,remain);
        let blank:cc.Sprite = cc.find("blank",node).getComponent(cc.Sprite);
        let num:cc.Label = cc.find("num",node).getComponent(cc.Label);
        let light:cc.Node = cc.find("light",node);
        this.removeCountdown(seat);
        let after = remain;
        blank.fillRange = (after/time);
        num.string = after.toString();
        GameAction_nbddz.lightPosition(light,after/time);
        light.active = true;

        this.countdownHandler[seat] = setInterval(function () {
            if(node == null) this.removeCountdown(seat);
            after = after - 0.1;
            if(after <=0){
                blank.fillRange = 0;
                num.string = "0";
                GameAction_nbddz.lightPosition(light,0);
                light.active = false;
                node.active=false;
                this.removeCountdown(seat)
            }else{
                blank.fillRange = (after/time);
                num.string = Math.floor(after).toString();
                GameAction_nbddz.lightPosition(light,after/time);
            }
        }.bind(this),100);
    }

    static removeCountdown(seat){
        if(this.countdownHandler[seat]){
            clearInterval(this.countdownHandler[seat]);
            this.countdownHandler[seat] = null;
        }
    }
}